import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# 设置全局字体为 Times New Roman
plt.rcParams['font.family'] = 'Times New Roman'

import pandas as pd

# 创建数据字典
data = {
    'Model': ['DeepVS [61]', 'ACLNet [46]', 'SalEMA [15]', 'STRA-Net [11]', 'TASED-Net [67]', 'Chen et al. [14]', 'SalSAC [13]', 'UNISAL [72]', 'MATNet [64]', 'STRNN [62]', 'ECANet [66]', 'HD2S [21]', 'STA3D [20]', 'ViNet [18]', 'TSFP-Net [17]', 'STSANet [16]', 'Ours'],
    'AUC-J↑ (DHF1K)': [0.856, 0.890, 0.890, 0.895, 0.895, 0.900, 0.896, 0.901, 0.904, 0.904, 0.903, 0.908, 0.908, 0.908, 0.912, 0.913, 0.915],
    'SIM ↑ (DHF1K)': [0.256, 0.315, 0.466, 0.355, 0.361, 0.353, 0.357, 0.390, 0.391, 0.365, 0.385, 0.406, 0.390, 0.381, 0.392, 0.383, 0.407],
    's-AUC↑ (DHF1K)': [0.583, 0.601, 0.667, 0.663, 0.712, 0.680, 0.697, 0.691, 0.686, 0.683, 0.717, 0.700, 0.721, 0.729, 0.723, 0.723, 0.731],
    'CC ↑ (DHF1K)': [0.344, 0.434, 0.449, 0.458, 0.470, 0.476, 0.479, 0.490, 0.490, 0.482, 0.500, 0.503, 0.515, 0.511, 0.517, 0.529, 0.546],
    'NSS ↑ (DHF1K)': [1.911, 2.354, 2.574, 2.558, 2.667, 2.685, 2.673, 2.776, 2.764, 2.692, 2.814, 2.812, 2.877, 2.872, 2.967, 3.010, 3.146],
    'AUC-J↑ (Hollywood-2)': [0.887, 0.886, 0.919, 0.923, 0.918, 0.928, 0.931, 0.934, 0.915, 0.925, 0.929, 0.936, 0.927, 0.930, 0.936, 0.938, 0.940],
    'SIM ↑ (Hollywood-2)': [0.356, 0.542, 0.487, 0.536, 0.507, 0.537, 0.529, 0.542, 0.539, 0.491, 0.526, 0.551, 0.534, 0.550, 0.571, 0.579, 0.607],
    'CC ↑ (Hollywood-2)': [0.446, 0.623, 0.613, 0.662, 0.646, 0.661, 0.670, 0.673, 0.674, 0.645, 0.673, 0.670, 0.659, 0.693, 0.711, 0.721, 0.739],
    'NSS ↑ (Hollywood-2)': [2.313, 3.086, 3.186, 3.478, 3.302, 3.804, 3.356, 3.901, 3.486, 3.279, 3.380, 3.352, 3.329, 3.730, 3.910, 3.927, 4.095],
    'AUC-J↑ (UCF Sports)': [0.870, 0.897, 0.906, 0.910, 0.899, 0.917, 0.926, 0.918, 0.901, 0.903, 0.924, 0.904, 0.900, 0.924, 0.923, 0.936, 0.936],
    'SIM ↑ (UCF Sports)': [0.321, 0.406, 0.431, 0.479, 0.469, 0.494, 0.534, 0.523, 0.503, 0.431, 0.522, 0.507, 0.465, 0.522, 0.561, 0.560, 0.565],
    'CC ↑ (UCF Sports)': [0.405, 0.510, 0.544, 0.593, 0.582, 0.599, 0.671, 0.644, 0.625, 0.569, 0.673, 0.604, 0.560, 0.673, 0.685, 0.705, 0.707],
    'NSS ↑ (UCF Sports)': [2.089, 2.567, 2.638, 3.018, 2.920, 3.406, 3.523, 3.381, 3.291, 2.791, 3.620, 3.114, 2.754, 3.620, 3.698, 3.908, 3.863]
}



# 创建并排序DataFrame
df = pd.DataFrame(data)

def sort_and_position_ours(df):
    sorted_df = df.sort_values(by='AUC-J↑ (DHF1K)', ascending=True)
    ours = sorted_df[sorted_df['Model'] == 'Ours']
    sorted_df = sorted_df[sorted_df['Model'] != 'Ours']
    return pd.concat([sorted_df, ours])

sorted_df = sort_and_position_ours(df)

# 计算颜色
colors = {}
for col in sorted_df.columns[1:]:  # 跳过第一列Model
    series = sorted_df[col]
    ranks = series.rank(ascending=False, method='min')
    col_colors = []
    for r in ranks:
        if r == 1:
            col_colors.append('#FF0000')  # 纯红色文本
        elif r == 2:
            col_colors.append('#00FF00')  # 纯绿色文本
        elif r == 3:
            col_colors.append('#0000FF')  # 纯黄色文本
        else:
            col_colors.append('black')    # 其他黑色
    colors[col] = col_colors

# 绘制表格
fig, ax = plt.subplots(figsize=(20, 12))
ax.axis('off')

# 创建表格数据
cell_text = []
for row in sorted_df.itertuples(index=False):
    cell_text.append(list(row))

# 设置表格（移除行标签）
table = ax.table(
    cellText=cell_text,
    colLabels=sorted_df.columns,
    cellLoc='center',
    loc='center',
)

# 设置文本颜色（跳过第一列Model）
for i in range(1, len(sorted_df.columns)):  # 列索引从1开始
    col_name = sorted_df.columns[i]
    for j in range(len(sorted_df)):
        color = colors[col_name][j]
        table[(j+1, i)].get_text().set_color(color)

# 设置模型名称列的样式
for j in range(len(sorted_df)):
    table[(j+1, 0)].set_facecolor('#F0F0F0')  # 浅灰色背景
    table[(j+1, 0)].get_text().set_color('black')


# 调整表格样式
table.auto_set_font_size(False)
table.set_fontsize(10)
table.scale(1.2, 1.5)  # 调整表格大小

# 一键加粗所有文字
for (row, col), cell in table.get_celld().items():
    cell.set_text_props(weight='bold')  # 全局加粗

# 优化布局
plt.tight_layout()
plt.savefig('comparison_table_v2.png', dpi=300, bbox_inches='tight', pad_inches=0.2)
plt.close()

